﻿using System;
using FeatureDemo.Helper;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Info;

namespace FeatureDemo.Features.Device
{
    [Feature("Device Status", "Checking the status of device", 7.5)]
    public partial class DeviceStatusPage : PhoneApplicationPage
    {
        public DeviceStatusPage()
        {
            InitializeComponent();

            // Set up the event handlers
            DeviceStatus.PowerSourceChanged += new EventHandler(DeviceStatus_PowerSourceChanged);
            DeviceStatus.KeyboardDeployedChanged += new EventHandler(DeviceStatus_KeyboardDeployedChanged);

            // Output initial values
            deviceManufacturerTextBlock.Text = DeviceStatus.DeviceManufacturer;
            deviceNameTextBlock.Text = DeviceStatus.DeviceName;
            deviceFirmwareVersionTextBlock.Text = DeviceStatus.DeviceFirmwareVersion;
            deviceHardwareVersionTextBlock.Text = DeviceStatus.DeviceHardwareVersion;
            deviceTotalMemoryTextBlock.Text = DeviceStatus.DeviceTotalMemory.ToString();
            appCurrentMemoryUsageTextBlock.Text = DeviceStatus.ApplicationCurrentMemoryUsage.ToString();
            appMemoryUsageLimitTextBlock.Text = DeviceStatus.ApplicationMemoryUsageLimit.ToString();
            appPeakMemoryUsageTextBlock.Text = DeviceStatus.ApplicationPeakMemoryUsage.ToString();
            isKeyboardPresentTextBlock.Text = DeviceStatus.IsKeyboardPresent.ToString();
            isKeyboardDeployedTextBlock.Text = DeviceStatus.IsKeyboardDeployed.ToString();
            powerSourceTextBlock.Text = DeviceStatus.PowerSource.ToString();
        }


        void DeviceStatus_PowerSourceChanged(object sender, EventArgs e)
        {
            // The PowerSourceChanged event is not raised on the UI thread, 
            // so the Dispatcher must be invoked to update the Text property.
            this.Dispatcher.BeginInvoke(() =>
            {
                powerSourceTextBlock.Text = DeviceStatus.PowerSource.ToString();
            }
            );
        }

        void DeviceStatus_KeyboardDeployedChanged(object sender, EventArgs e)
        {
            // The KeyboardDeployedChanged event is not raised on the UI thread, 
            // so the Dispatcher must be invoked to update the Text property.
            this.Dispatcher.BeginInvoke(() =>
            {
                isKeyboardDeployedTextBlock.Text = DeviceStatus.IsKeyboardDeployed.ToString();
            }
            );
        }
    }
}